package biz.bbtec.sbm.client.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import biz.bbtec.sbm.client.bean.BusinessBean;
import biz.bbtec.sbm.client.bean.ClientBean;
import biz.bbtec.sbm.client.factory.BusinessFactory;
import biz.bbtec.sbm.client.service.BusinessService;
import biz.bbtec.sbm.user.bean.UserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BusinessAction extends ActionSupport implements SessionAware {

	private BusinessBean business = new BusinessBean();
	private List<BusinessBean> businesslist = new ArrayList<BusinessBean>();
	private ClientBean client;
	private int userid;
	private int pageNow;
	public int getPageNow(){
		return this.pageNow;
	}
	public void setPageNow(int pageNow){
		this.pageNow=pageNow;
	}
	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	private List<UserBean> userlist = new ArrayList<UserBean>();

	public List<UserBean> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<UserBean> userlist) {
		this.userlist = userlist;
	}

	private Map session;

	public ClientBean getClient() {
		return client;
	}

	public void setClient(ClientBean client) {
		this.client = client;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public BusinessBean getBusiness() {
		return business;
	}

	public void setBusiness(BusinessBean business) {
		this.business = business;
	}

	public List<BusinessBean> getBusinesslist() {
		return businesslist;
	}

	public void setBusinesslist(List<BusinessBean> businesslist) {
		this.businesslist = businesslist;
	}

	public String showallBusiness() throws Exception {
		if (business.getClientid() != 0) {
			client = new ClientBean();
			client.setClientid(business.getClientid());
		}
		/*else{
			client=new ClientBean();
			client.setClientid((Integer)session.get("clientid"));
		}*/
		BusinessService businesservice = BusinessFactory.getInstance()
				.creatBusinessService();
		int total=businesservice.CountAllBusiness(client);
		businesslist = businesservice.showallbusiness(client,getPageNow(),15);
		if(getPageNow()==0){
			int page=0;
			do{
				total=total-15;
				page++;
			}while(total>0);
			this.session.put("businesstotalpage", page);
		}
		if (businesslist.size() == 0) {
			this.addActionError("您没有任何业务，请添加！");
			//System.out.println(businesslist.size()+"!!"+client.getClientid());
			if(client!=null){
			this.session.put("clientid", client.getClientid());}
			return INPUT;
		} else {
			int clientid = businesslist.get(0).getClientid();
			this.session.put("clientid", clientid);
			return SUCCESS;
		}
	}

	public String ShowUsers() {
		BusinessService businesservice = BusinessFactory.getInstance()
				.creatBusinessService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		userlist = businesservice.showUsers(user);
		System.out.println("cai" + userlist.size());
		if (userlist.size() == 0) {
			this.addActionError("出错啦！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String addBusiness() {
		BusinessService businesservice = BusinessFactory.getInstance()
				.creatBusinessService();
		business.setUserid(getUserid());
		if (business.getName() == null) {
			this.addActionError("业务名称不能为空");
			return INPUT;
		}
		if (!(businesservice.addbusiness(business))) {
			this.addActionError("添加失败！");
			return INPUT;
		}
		this.addActionError("添加成功！");
		return SUCCESS;
	}

	public String deleteBusiness() {
		BusinessService businesservice = BusinessFactory.getInstance()
				.creatBusinessService();
		if (!(businesservice.deletebusiness(business))) {
			this.addActionError("ERROR!");
			return INPUT;
		}
		return SUCCESS;
	}

	public String modifyBusiness() {
		if (business.getName() == null) {
			this.addActionError("业务名称不能为空！");
			return INPUT;
		}
		if(business.getClientid() == 0){
			Map session = ActionContext.getContext().getSession();
			int clientid = (Integer) session.get("clientid");
			business.setClientid(clientid);
		}
		BusinessService businesservice = BusinessFactory.getInstance()
				.creatBusinessService();
		if (!(businesservice.modifybusiness(business))) {
			this.addActionError("ERROR!");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		session.remove("business");
		return SUCCESS;
	}

	public String showoneBusiness() throws Exception {
		if (business.getBizid() == 0) {
			Map session = ActionContext.getContext().getSession();

			int bizid = (Integer) session.get("bizid");
			int clientid = (Integer) session.get("clientid");
			business.setBizid(bizid);
			business.setClientid(clientid);
		}

		if (business.getClientid() == 0) {
			client = new ClientBean();
			client.setClientid(business.getClientid());
		}

		BusinessService businessservice = BusinessFactory.getInstance()
				.creatBusinessService();
		business = businessservice.showonebusiness(business);
		if (business == null) {
			this.addActionError("Error!");
			return INPUT;
		} else {
			this.session.put("bizid", business.getBizid());
			this.session.put("clientid", business.getClientid());
			this.session.put(business, "business");
			return SUCCESS;
		}
	}

}
